using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TVDesigner.Entities.Elements;


namespace TVDesigner.GUI.Elements
{
    public partial class TextElement : Element
    {
        private static int textCount = 0;

        public TextElement() : this(null)
        {
            
        }

        public TextElement(HText text)
        {
            InitializeComponent();
            AddEvents(_Label);
            if (text != null)
            {
                _LogicElement = text;
                this.Name = text.Name;
                this.Size = text.Size;
                this.Location = text.Location;
                _Label.ForeColor = text.Foreground;
                _Label.BackColor = text.Background;
                _Label.TextAlign = text.TextAlign;
                _Label.Font = text.Font;
            }
            else
            {
                HText temp = new HText();
                temp.Size = this.Size;
                temp.Location = this.Location;
                this.Name = "text" + textCount++;
                temp.Name = this.Name;

                temp.Text = this.Name;
                temp.Background = _Label.BackColor;
                temp.Foreground = _Label.ForeColor;
                temp.TextAlign = _Label.TextAlign;
                temp.Font = _Label.Font;

                _LogicElement = temp;
            }

            _Label.Text = this.Name;
        }


        #region Validacao das propriedades

        public override void PropertyGridValueChanged(PropertyGrid propertyGrid, PropertyValueChangedEventArgs e)
        {
            base.PropertyGridValueChanged(propertyGrid, e);

            if (e.ChangedItem.Label == "Text" && !_ValidateText((string)e.ChangedItem.Value))
            {
                e.ChangedItem.PropertyDescriptor.SetValue(
                    propertyGrid.SelectedObject, e.OldValue);
            }
            else if (e.ChangedItem.Label == "Foreground" && !_ValidateForeGround((Color)e.ChangedItem.Value))
            {
                e.ChangedItem.PropertyDescriptor.SetValue(
                    propertyGrid.SelectedObject, e.OldValue);
            }
            else if (e.ChangedItem.Label == "Background" && !_ValidateBackground((Color)e.ChangedItem.Value))
            {
                e.ChangedItem.PropertyDescriptor.SetValue(
                    propertyGrid.SelectedObject, e.OldValue);
            }
            //else if (e.ChangedItem.Label == "Alpha" && !_ValidateAlpha((double)e.ChangedItem.Value))
            //{
            //    e.ChangedItem.PropertyDescriptor.SetValue(
            //        propertyGrid.SelectedObject, e.OldValue);
            //}
            else if (e.ChangedItem.Label == "TextAlign" && !_ValidateTextAlign((ContentAlignment)e.ChangedItem.Value))
            {
                e.ChangedItem.PropertyDescriptor.SetValue(
                    propertyGrid.SelectedObject, e.OldValue);
            }
            else if (e.ChangedItem.Label == "Font" && !_ValidateFont((Font)e.ChangedItem.Value))
            {
                e.ChangedItem.PropertyDescriptor.SetValue(
                    propertyGrid.SelectedObject, e.OldValue);
            }
        }

        private bool _ValidateFont(Font newFont)
        {
            _Label.Font = newFont;
            ((HText)_LogicElement).Font = newFont;
            return true;
        }

        private bool _ValidateTextAlign(ContentAlignment contentAlignment)
        {
            _Label.TextAlign = contentAlignment;
            ((HText)_LogicElement).TextAlign = contentAlignment;
            return true;
        }

        private bool _ValidateBackground(Color color)
        {
            bool valid = true;
            HText htext = this.LogicElement as HText;
            htext.Background = color;
            _Label.BackColor = color;
            return valid;
        }

        //private bool _ValidateAlpha(double alpha)
        //{
        //    bool valid = true;

        //    if (alpha >= 0 && alpha <= 255)
        //    {
        //        HText htext = this.LogicElement as HText;
        //        htext.Alpha = alpha;
        //    }
        //    else
        //    {
        //        valid = false;
        //    }
        //    return valid;
        //}

        private bool _ValidateForeGround(Color color)
        {
            bool valid = true;
            HText htext = this.LogicElement as HText;
            htext.Foreground = color;
            _Label.ForeColor = color;
            return valid;
        }

        private bool _ValidateText(string text)
        {
            bool valid = true;
            HText htext = this.LogicElement as HText;
            htext.Text = text;
            _Label.Text = text;
            return valid;
        } 
        #endregion

    }
}

